#ifndef __DRICE_EVENT_HANDLE_H__
#define __DRICE_EVENT_HANDLE_H__
#include "stdafx.h"
#include "nfapi.h"
#include "UdpProxy.h"
#include "Events.h"

using namespace nfapi;
//
//	API events handler
//
class DriveEventHandler : public NF_EventHandler,
                          public UdpProxy::UDPProxyHandler
{
    TcpProxy::LocalTCPProxy m_tcpProxy;
    ProxyEventHandler m_proxyHandler;
    typedef std::map<ENDPOINT_ID, UDP_CONTEXT *> tUdpCtxMap;
    tUdpCtxMap m_udpCtxMap;
#ifdef _CFG_UDP_
    UdpProxy::UDPProxy m_udpProxy;
#endif
    typedef std::set<ENDPOINT_ID> tIdSet;
    tIdSet m_filteredUdpIds;

    AutoCriticalSection m_cs;

public:
    bool init();
    void free();
    virtual void onUdpReceiveComplete(ENDPOINT_ID id, char *buf, int len, char *remoteAddress, int remoteAddressLen);
    virtual void threadStart();
    virtual void threadEnd();

    //
    // TCP events
    //

    virtual void tcpConnectRequest(ENDPOINT_ID id, PNF_TCP_CONN_INFO pConnInfo);
    virtual void tcpConnected(ENDPOINT_ID id, PNF_TCP_CONN_INFO pConnInfo);
    virtual void tcpClosed(ENDPOINT_ID id, PNF_TCP_CONN_INFO pConnInfo);
    virtual void tcpReceive(ENDPOINT_ID id, const char *buf, int len);
    virtual void tcpSend(ENDPOINT_ID id, const char *buf, int len);
    virtual void tcpCanReceive(ENDPOINT_ID id);
    virtual void tcpCanSend(ENDPOINT_ID id);

    //
    // UDP events
    //

    virtual void udpCreated(ENDPOINT_ID id, PNF_UDP_CONN_INFO pConnInfo);
    virtual void udpConnectRequest(ENDPOINT_ID id, PNF_UDP_CONN_REQUEST pConnReq);
    virtual void udpClosed(ENDPOINT_ID id, PNF_UDP_CONN_INFO pConnInfo);
    virtual void udpReceive(ENDPOINT_ID id, const unsigned char *remoteAddress, const char *buf, int len, PNF_UDP_OPTIONS options);
    virtual void udpSend(ENDPOINT_ID id, const unsigned char *remoteAddress, const char *buf, int len, PNF_UDP_OPTIONS options);
    virtual void udpCanReceive(ENDPOINT_ID id);
    virtual void udpCanSend(ENDPOINT_ID id);
};

#endif // !__DRICE_EVENT_HANDLE_H__
